
#include "CMineCrystalConfig.h"
#include "../mainctrl.h"

int CMineDrop::AddDropData(unsigned char ucMineType, unsigned char ucPos,int iDropRate)
{
    if ((ucMineType <= MINE_TYPE_NONE)
        || (ucMineType > MINE_TYPE_MAX))
    {
        return ERROR_MINE_CRYSTAL_CONFIG;
    }

    CRateTarget stDrop;
    stDrop._iTargetID = ucPos;
    stDrop._iMaxCount = CRateTarget::TARGET_INFINITY;
    stDrop._iRate = iDropRate;

    _stMineDrop[ucMineType-1].Add(stDrop);
    return 0;
}

int CMineDrop::DropItem (unsigned char ucMineType, unsigned char &ucPos)
{   
    if ((ucMineType <= MINE_TYPE_NONE)
        || (ucMineType > MINE_TYPE_MAX))
    {
        return ERROR_MINE_TYPE_INVALID;
    }
    
    CRateTarget stDrop;
    int iRet = _stMineDrop[ucMineType-1].DropOne(stDrop);
    if (iRet)
    {
        return ERROR_MINE_DROP_NONE;
    }

    ucPos = (unsigned char)(stDrop._iTargetID & 0x7F);
    return 0;
}

int CMineCrystalConfig::AddConfig (unsigned char ucMineType, CMineDropItemConfig & stConfig, int iDropRate)
{
    if ((ucMineType <= MINE_TYPE_NONE)
        || (ucMineType > MINE_TYPE_MAX))
    {
        return ERROR_MINE_TYPE_INVALID;
    }

    unsigned char ucPos;
    int iRet = _stMineConfig[ucMineType - 1].AddDropItem(stConfig, ucPos);
    if (iRet)
    {
        CT_ERROR(("%d, %d, %d", ucMineType, stConfig._ucItemType, iRet));
        return iRet;
    }

    iRet = _stMineDrop.AddDropData(ucMineType, ucPos, iDropRate);
    if (iRet)
    {
        CT_ERROR(("%d, %d, %d, %d", ucMineType, ucPos, iDropRate, iRet));
        return iRet;
    }

    return 0;
}

int CMineCrystalConfig::DropItem (unsigned char ucMineType, CDropItemData & stDropData)
{
    unsigned char ucPos;
    int iRet = _stMineDrop.DropItem(ucMineType, ucPos);
    if (iRet)
    {
        CT_ERROR(("%d, %d", ucMineType, iRet));
        return iRet;
    }

    CMineDropItemConfig stConfigData;
    iRet = _stMineConfig[ucMineType - 1].GetDropItem(ucPos, stConfigData);
    if (iRet)
    {
        CT_ERROR(("%d, %d", ucPos, iRet));
        return iRet;
    }

    stDropData._ucItemType = stConfigData._ucItemType;
    stDropData._iItemCount = stConfigData._ucCount;
    if (0 != stConfigData._usItemId)
    {
        stDropData._usItemConfigID = stConfigData._usItemId;
    }
    else
    {
        if (ITEM_TYPE_CRYSTAL == stConfigData._ucItemType)
        {
            CCrystalConfigData stCrystalCfg;
            iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetCrystalConfig(stConfigData._szItemSubType, stCrystalCfg);
            if (iRet)
            {
                CT_ERROR(("%s, %d", stConfigData._szItemSubType.GetBuffer(), iRet));
                return iRet;
            }

            stDropData._usItemConfigID = stCrystalCfg._usCrystalConfigID;
        }
        else if (ITEM_TYPE_JEWEL == stConfigData._ucItemType)
        {
            CJewlConfigData stJewlCfg;
            iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetJewelConfig(stConfigData._szItemSubType, stJewlCfg);
            if (iRet)
            {
                CT_ERROR(("%s, %d", stConfigData._szItemSubType.GetBuffer(), iRet));
                return iRet;
            }

            stDropData._usItemConfigID = stJewlCfg._usJewelId;
        }
        else
        {
            CT_ERROR((""));
            return ERROR_MINE_CRYSTAL_CONFIG;
        }
    }

    return 0;
}
